<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * event CRUD model class 
 */
Class Event_model extends CI_Model {

    public $events_table;
    public $programsav_table;
    public $film_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->events_table = $this->db->dbprefix('esemeny');
        $this->programsav_table = $this->db->dbprefix('programsav');
        $this->film_table = $this->db->dbprefix('filmek');
    }

    /**
     * 
     * 
     * 
     * Update event by id.
     * 
     * 
     * @return true on success
     */
    function update_event($event_data = array()) {
        // if event does not exists throw event_not_exists_exception
        if ($this->get_event($event_data['esemeny_id']) == FALSE)
            throw new not_exists_exception("event");

        // do selection and update data
        $this->db->where('esemeny_id', $event_data['esemeny_id']);
        $this->db->update($this->events_table, $event_data);
    }

    /**
     * 
     * 
     * 
     * Delete event
     * 
     * 
     * 
     * @param type $id
     */
    function delete($esemeny_id) {
        // delete event data from events_table
        $this->db->where('esemeny_id', $esemeny_id);
        $this->db->delete($this->events_table);
    }

    /**
     * 
     * Add new event
     * 
     * @param type $event_data
     */
    function add_event($event_data = array()) {
        // insert event_data
        $this->db->insert($this->events_table, $event_data);
    }

    /**
     * 
     * returns the events (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned events
     * @param type $offset from this number.
     * @return type 
     */
    function get_events($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (strpos($options['select'], 'programsav_nev')) {
            // programsav_join
            $this->db->join($this->programsav_table, "$this->programsav_table.programsav_id = $this->events_table.esemeny_programsav_id", 'left');
        }
        if (strpos($options['select'], 'eredeti_cim')) {
            // eventek join
            $this->db->join($this->film_table, "$this->film_table.film_id = $this->events_table.esemeny_film_id", 'left');
        }

        if (isset($options['search_like'])) {
            $this->db->like('esemeny_cim', $options['search_like']);
            $this->db->or_like('esemeny_kezdes', $options['search_like']);
            $this->db->or_like('esemeny_vege', $options['search_like']);
            $this->db->or_like('esemeny_leiras', $options['search_like']);
            $this->db->or_like('programsav_nev', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->events_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of events
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->events_table);
    }

    /**
     * Gives back event by id 
     * 
     * @param type $id
     * @return array (esemeny_id, eventname, event)
     */
    function get_event($id) {
        // constructing active record query
        $query = $this->db->get_where($this->events_table, array('esemeny_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * Gives back event by slug
     * 
     * @param type $slug
     * @return array (event_id, eventname, event)
     */
    function get_event_by_slug($slug, $ignore_id = NULL) {
        // constructing active record query
        // ignore id
        if ($ignore_id) {
            $this->db->where_not_in('esemeny_id', $ignore_id);
        }
        $query = $this->db->get_where($this->events_table, array('esemeny_cim_slug' => $slug));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * 
     * visszaadja az eseményeket a range-ben
     * 
     */
    function get_in_range($start, $end) {
        $this->db->select('esemeny_cim, esemeny_cim_slug, esemeny_kezdes, esemeny_vege, programsav_szin, esemeny_helyszin');
        
        $this->db->where('esemeny_kezdes >', $start);
        $this->db->or_where('esemeny_vege <', $end);
        
        $this->db->join($this->programsav_table, "$this->events_table.esemeny_programsav_id = $this->programsav_table.programsav_id", "left");
        
        $query = $this->db->get($this->events_table);
        $result = $query->result();
        
        return $result;
    }

}